% Solve for the first best allocation

temp.xi    = fzero(@(x) sum(pai.*pr.Om^(-1/pr.sig).*(x./pw).^(1/pr.sig).*w.^((1+pr.sig)/pr.sig)) ...
                       -sum(pai.*(pw./x).^(1/pr.gam))-G ,1); % Lagrange multiplier
fb.c       = (pw./temp.xi).^(1/pr.gam);
fb.y       = pr.Om^(-1/pr.sig).*(temp.xi./pw).^(1/pr.sig).*w.^((1+pr.sig)/pr.sig);
fb.output  = sum(pai.*fb.y);
fb.welfare = sum(pw.*pai.*Utility(fb.c,fb.y,w,pr));
fb.MTR     = 1-pr.Om./fb.c.^(-pr.gam).*w.^-(1+pr.sig).*fb.y.^pr.sig;
fb.ATR     = (fb.y-fb.c)./fb.y;
fb.WelfareGain = 100*fzero(@(x) sum(pw.*pai.*Utility((1+x).*baseline.c,baseline.y,w,pr))-fb.welfare,.5);
fb.OutputLoss  = 100*(fb.output/baseline.output-1);
fb.EfAveMTR    = sum(pai.*fb.MTR.*fb.y/fb.output);
[temp.y,temp.i]= min(fb.y);
temp.c         = fb.c(temp.i);
fb.Tr2Y        = (temp.c-temp.y)./fb.output;
fb.TrG2Y       = (temp.c-temp.y+G)./fb.output;

save ./variables/fb fb
clear temp